﻿#region Copyright & License
/* Copyright 2001-2005 (C) Lilly Corporation.  All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Neither the name of the PixVillage Team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written 
permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

using log4net;
using log4net.Core;
using log4net.Config;

namespace EAI.Logging
{
    /// <summary>
    /// This is the class used by client applications to log the message.
    /// </summary>
    /// <example>Simple example of using LogHelp
    /// <code>
    ///  try
    ///  {
    ///           
    ///  }
    ///  catch (Exception ex)
    ///  {
    ///      LogHelp.LogHelp.GetLogger().Error("error", ex);
    ///  }
    /// </code>
    /// </example>
    public static class Log4NetHelper
    {
        /// <summary>
        /// The static constructor to initialize the log4net configuration.
        /// </summary>
        static Log4NetHelper()
        {
            string log4netConfig = Path.Combine(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath != null ? AppDomain.CurrentDomain.SetupInformation.PrivateBinPath : AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"Config\log4net.xml");

            // Initialise the log4net system using the DOM Configurator.
            // The config settings are read from the "Config/log4net.xml" file.
            XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(log4netConfig));
        }

        /// <summary>
        /// Gets the default logger instance.
        /// </summary>
        /// <returns>The ILog instance.</returns>
        public static IEAILog GetLogger()
        {
            Assembly assembly = Assembly.GetEntryAssembly();
           
            if (assembly == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }

            if (assembly == null)
            {
                assembly = Assembly.GetExecutingAssembly();
            }

            return EAILogManager.GetLogger(assembly.FullName);
        }

        /// <summary>
        /// Gets the logger instance with specify name.
        /// </summary>
        /// <param name="name">The logger name.</param>
        /// <returns>The ILog instance.</returns>
        public static IEAILog GetLogger(string name)
        {
            return EAILogManager.GetLogger(name);
        }     
    }
}
